*************************
clear all
set more off
capture log close
set logtype text
cd t:\vega\kilian_vega
set memory 600m
**************************

***output is stored in test2.log file
log using test2, replace
log close

use wti_clean
sort date
save wti_clean, replace

use mms_all_final2, clear
sort date

merge date using wti_clean
sort date
foreach num of numlist 26 27 28 1 7 9 11 10 19 21 17 18 3 13 15 12 20 6 8 34 2 33 71 72 14 16 4 5 32 25 {

*This deals with the problem of missing price change data on a given day; it drops the announcement down to the next day.
replace surs`num'=surs`num'[_n-1] if surs`num' ==. & wti[_n-1] ==. & wti!=. & date-date[_n-1]<=3

}

drop if wti==.
 
*GDP Advanced, GDP Preliminary, GDP Final, Unemployment Rate, Nonfarm Payroll, Retail Sales, Industrial Production, Capacity Utilization, Personal Income, Consumer Credit, Personal Consumption Expenditures, New Home Sales, Durable Goods Orders, Construnction Spending, Factory Orders, Business Inventories, Government Budget Deficit, Trade Balance, PPI, Core PPI, CPI, Core CPI, Consumer Confidence Index (Board), Consumer Confidence (U. Mich.), NAPM Index, Housing Starts, Index of Leading Indicators, Target Rate Surprises, Initial Claims

*Now I am turning all the missing values into zero to run the regression:
foreach num of numlist 26 27 28 1 7 9 11 10 19 21 17 18 3 13 15 12 20 6 8 34 2 33 71 72 14 16 4 5 32 25 {
gen ann`num'=(surs`num'~=.)

replace surs`num'=0 if surs`num'==.

}

sort date
gen price_ch_w=100*(log(wti[_n+20])-log(wti[_n-1]))
keep date price_ch_w surs26 surs27 surs28 surs1 surs7 surs9 surs11 surs10 surs19 surs21 surs17 surs18 surs3 surs13 surs15 surs12 surs20 surs6 surs8 surs34 surs2 surs33 surs71 surs72 surs14 surs16 surs4 surs5 surs32 surs25 ann26 ann27 ann28 ann1 ann7 ann9 ann11 ann10 ann19 ann21 ann17 ann18 ann3 ann13 ann15 ann12 ann20 ann6 ann8 ann34 ann2 ann33 ann71 ann72 ann14 ann16 ann4 ann5 ann32 ann25
save lutz_wti_monthly, replace

foreach num of numlist 26 27 28 1 7 9 11 10 19 21 17 18 3 13 15 12 20 6 8 34 2 33 71 72 14 16 4 5 32 25 {
use lutz_wti_monthly, clear
keep surs`num' ann`num' price_ch_w date
keep if ann`num'==1
sort date
gen time=_n
tsset time	

reg price_ch_w surs`num'
egen vary=sd(price_ch_w)
replace vary=vary^2
egen varx=sd(surs`num')
replace varx=varx^2
global vary=vary[1]
global varx=varx[1]

global dep=e(N)-e(df_r)
global adj_r2=round(e(r2)*100,0.01)
global obs=e(N)
global depname=e(depvar)
global numvar=`num'


newey price_ch_w surs`num', lag(5)

matrix V=e(V)
global tstat=round(_b[surs`num']/sqrt(V[1,1]),0.01)
global pneg=1-ttail($obs-1,_b[surs`num']/sqrt(V[1,1]))
global ppos=ttail($obs-1,_b[surs`num']/sqrt(V[1,1]))
*global pval=ttail($obs-1,_b[surs`num']/sqrt(V[1,1]))

disp $pval $tstat
do record5.do

egen sd_ann=sd(surs`num')
egen sd_p=sd(price_ch_w)
egen obs=count(surs`num')
gen str30 variable="wti_`num'"
gen var_price=sd_p^2
gen var_surs=sd_ann^2
keep if _n==1
keep var_price var_surs variable obs
save test`num', replace
}

use lutz_wti_monthly, clear
keep surs26 surs27 surs28 surs1 surs7 surs9 surs11 surs10 surs19 surs21 surs17 surs18 surs3 surs13 surs15 surs12 surs20 surs6 surs8 surs34 surs2 surs33 surs71 surs72 surs14 surs16 surs4 surs5 surs32 surs25
order surs26 surs27 surs28 surs1 surs7 surs9 surs11 surs10 surs19 surs21 surs17 surs18 surs3 surs13 surs15 surs12 surs20 surs6 surs8 surs34 surs2 surs33 surs71 surs72 surs14 surs16 surs4 surs5 surs32 surs25
save lutz_wti_monthly, replace


foreach num of numlist 26 {
use test`num', clear
save wti_monthly, replace
}
foreach num of numlist 27 28 1 7 9 11 10 19 21 17 18 3 13 15 12 20 6 8 34 2 33 71 72 14 16 4 5 32 25 {
use wti_monthly, clear
append using test`num'
save wti_monthly, replace
}

use wti_monthly, clear
order var_price var_surs variable obs

 
save wti_monthly, replace


matrix results=results26
foreach num of numlist 27 28 1 7 9 11 10 19 21 17 18 3 13 15 12 20 6 8 34 2 33 71 72 14 16 4 5 32 25 {

matrix results=results\results`num'
}

log using test2, replace
matlist results
log close


